package gui;

import orm.MyBatisSQLSession;
import orm.User;
import view.MainView;
import view.MainViewAction;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

public class AppWindow extends JFrame {
    MainView mainView;
    JPanel jp0, jp1, jp2, jp3;
    JLabel jlb1, jlb2;
    JButton jb1, jb2;
    JTextField jtf1;
    JPasswordField jpf1;
   
    public static void main(String[] args) {
        try {
           // org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FontUIResource fontResource = new FontUIResource(MainView.labelFont);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontResource);
            }
        }
        AppWindow log = new AppWindow();
        log.login();
    }

    public void login() {
        jp0 = new JPanel();
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        jlb1 = new JLabel("账号：");
        jlb2 = new JLabel("密码：");
        jb1 = new JButton("登录");
        jb2 = new JButton("取消");

        jtf1 = new JTextField(15);
        jpf1 = new JPasswordField(15);
        // 设置布局管理
        this.setLayout(new GridLayout(4, 1));

        jp1.add(jlb1);
        jp1.add(jtf1);

        jp2.add(jlb2);
        jp2.add(jpf1);

        jp3.add(jb1);
        jp3.add(jb2);
        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                char[] password = jpf1.getPassword();
                CharBuffer charBuffer = CharBuffer.allocate(password.length);
                charBuffer.put(password);
                charBuffer.flip();
                byte[] bytes = StandardCharsets.UTF_8.encode(charBuffer).array();
                try {
                    bytes = MessageDigest.getInstance("MD5").digest(bytes);
                } catch (NoSuchAlgorithmException e1) {
                    e1.printStackTrace();
                }
                StringBuilder sb = new StringBuilder();
                for (byte aByte : bytes) {
                    String hex = Integer.toHexString(aByte & 0xFF);
                    if (hex.length() < 2) {
                        sb.append(0);
                    }
                    sb.append(hex);
                }
                String passwordStr = sb.toString();
                User user = new User(jtf1.getText(), passwordStr);
                user = MyBatisSQLSession.getUserMapper().select(user);
                if (user == null) {
                    JOptionPane.showMessageDialog(null, "账号或密码错误", "提示", JOptionPane.ERROR_MESSAGE);
                } else {
                    mainView.setVisible(true);
                    MainView.userId = user.getId();
                    mainView.setTitle("单词簿-" + user.getName());
                    MainViewAction.showHint(user.getName() + " 已登录");
                    dispose();
                    MainViewAction.refreshWords();
                }
            }
        });
        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtf1.setText(null);
                jpf1.setText(null);
            }
        });

        // 加入到JFrame
        this.add(jp0);
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);

        this.setTitle("登录界面");
        this.setSize(500, 300);
        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
        MyBatisSQLSession.init();

        mainView = new MainView();
    }
}
